package com.qianqian.service.config;

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;
import com.alibaba.fastjson.serializer.ValueFilter;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

@Configuration
public class WebConfig {

    @Bean
    public HttpMessageConverters customConverters() {
        FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
        FastJsonConfig fjc = new FastJsonConfig();
        //空值过滤器
        ValueFilter valueFilter = (obj, s, v) -> v == null ? "" : v;
//        ContextValueFilter c = new ContextValueFilter() {
//            @Override
//            public Object process(BeanContext context, Object object, String name, Object value) {
//                System.out.println(context);
//                System.out.println(object);
//                return value;
//            }
//        };
//        fjc.setSerializeFilters(valueFilter,c);
        fjc.setSerializeFilters(valueFilter);


        SerializeConfig mapping = new SerializeConfig();
        String dateFormat;
        dateFormat = "yyyy-MM-dd HH:mm:ss";
        mapping.put(Date.class, new SimpleDateFormatSerializer(dateFormat));
        fjc.setSerializeConfig(mapping);

        fjc.setSerializerFeatures(SerializerFeature.BrowserCompatible, SerializerFeature.WriteNullStringAsEmpty);
        fastJsonConverter.setFastJsonConfig(fjc);
        return new HttpMessageConverters(fastJsonConverter);
    }



}
